package com.walk.zworld.ui.fragment;

import android.annotation.SuppressLint;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;

import com.base.lib.base.view.BaseFragment;
import com.danikula.videocache.HttpProxyCacheServer;
import com.walk.zworld.R;
import com.walk.zworld.ui.video.DataCreate;
import com.walk.zworld.ui.video.VideoBean;

import java.util.List;

public class VideoLiveFragment extends BaseFragment {

    public static VideoLiveFragment newsInstance() {
        return new VideoLiveFragment();
    }
    private ImageButton likeButton, commentButton, shareButton;
    private TextView textView;
    private VideoView videoView;
    private GestureDetector gestureDetector;
    private View.OnTouchListener touchListener;
    private HttpProxyCacheServer proxy;
    private List<VideoBean> describe = DataCreate.datas;
    private int i = 0;
    private void checki(){
        if(i > describe.size()){
            i = 0;
        }else if(i < 0){
            i = 9;
        }
    }
    @Override
    public int onBindLayout() {
        return R.layout.activity_video_live;
    }

    @Override
    public void initView(@NonNull View mView) {
        // 初始化组件
        textView = findViewById(R.id.textView);
        videoView = findViewById(R.id.videoView);
        likeButton = findViewById(R.id.likeButton);
        commentButton = findViewById(R.id.commentButton);
        shareButton = findViewById(R.id.shareButton);
        GestureInit();
        // 自动播放
        initvideo();
        // 设置监听器
        setupListeners();
    }

    @Override
    public void initData() {

    }

    @SuppressLint("ClickableViewAccessibility")
    private void GestureInit(){
        gestureDetector = new GestureDetector(requireContext(), new GestureDetector.OnGestureListener() {
            private static final int SWIPE_THRESHOLD = 100;
            private static final int SWIPE_VELOCITY_THRESHOLD = 100;
            @Override
            public boolean onDown(@NonNull MotionEvent e) {
                videoView.performClick();
                return true;
            }
            @Override
            public void onShowPress(@NonNull MotionEvent e) {
            }
            @Override
            public boolean onSingleTapUp(@NonNull MotionEvent e) {
                return true;
            }
            @Override
            public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
                return true;
            }
            @Override
            public void onLongPress(@NonNull MotionEvent e) {

            }
            @Override
            public boolean onFling(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float velocityX, float velocityY) {
                float diffy = e2.getY() - e1.getY();
                if(Math.abs(diffy) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD){
                    if(diffy > 0){
                        //上滑
                        i = i + 1;
                        checki();
                        initvideo();
                        //Toast.makeText(MainActivity.this, "上滑", Toast.LENGTH_SHORT).show();
                    } else {
                        //下滑
                        i = i - 1;
                        checki();
                        initvideo();
                        //Toast.makeText(MainActivity.this, "下滑", Toast.LENGTH_SHORT).show();
                    }
                    return true;
                }
                return false;
            }
        });
        touchListener = (v, event) -> {
            v.performClick();
            return gestureDetector.onTouchEvent(event);
        };
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(requireActivity())
//                .maxCacheSize(1024*1024*1024)
//                .cacheDirectory(getCacheDir())
//                .maxCacheFilesCount(20)
                .build();
    }

    private void initvideo(){
        if (describe.size() == 0) {
            describe = new DataCreate().getDatas();
        }
        if(proxy == null) {
            proxy =newProxy();
        }
        String proxyurl = proxy.getProxyUrl(describe.get(i).getVideoRes());
        videoView.setVideoPath(proxyurl);
        videoView.start();
        textView.setText(describe.get(i).getContent());
    }
    @SuppressLint("ClickableViewAccessibility")
    private void setupListeners() {
        MediaController mc = new MediaController(requireActivity());
        videoView.setMediaController(mc);
        videoView.setOnTouchListener(touchListener);
        videoView.setOnClickListener(v -> {
            if(!mc.isShowing()){
                mc.show(3000);
            }
        });
        videoView.setOnCompletionListener(mp -> {i = i + 1; checki(); initvideo();});
        likeButton.setOnClickListener(v -> Toast.makeText(requireContext(), "点赞!", Toast.LENGTH_SHORT).show());
        commentButton.setOnClickListener(v -> Toast.makeText(requireContext(), "评论!", Toast.LENGTH_SHORT).show());
        shareButton.setOnClickListener(v -> { proxy =newProxy(); initvideo(); Toast.makeText(requireContext(), "分享!", Toast.LENGTH_SHORT).show();});
    }

    public void pauseVideo() {
        videoView.pause();
    }
}
